package com.inspur.cmis.controller;

import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.inspur.cmis.constant.Constant;
import com.inspur.cmis.pojo.MgrCertificate;
import com.inspur.cmis.pojo.MgrCheck;
import com.inspur.cmis.pojo.MgrCmrpr;
import com.inspur.cmis.pojo.MgrLearnTrain;
import com.inspur.cmis.pojo.MgrLevelAffirm;
import com.inspur.cmis.pojo.MgrWorkHistory;
import com.inspur.cmis.pojo.MgrWorkResult;
import com.inspur.cmis.pojo.Param;
import com.inspur.cmis.service.MgrCertificateService;
import com.inspur.cmis.service.MgrCheckService;
import com.inspur.cmis.service.MgrCmrprService;
import com.inspur.cmis.service.MgrLevelService;
import com.inspur.cmis.service.MgrltrService;

import com.inspur.cmis.service.MgrWorkHistoryService;
import com.inspur.cmis.service.MgrWorkResultService;
import com.inspur.cmis.service.ParamService;

@Controller
public class ClientMgrOtherInfoController {
	@Autowired
	private MgrWorkResultService mgrWorkResultService;
	@Autowired
	private MgrWorkHistoryService mgrWorkHistoryService;
	@Autowired
	private ParamService paramService;
	@Autowired
	private  MgrCertificateService mgrCertificateService;
	@Autowired
	private MgrCmrprService mgrCmrprService;
	@Autowired
	private MgrCheckService mgrCheckService;
		@Autowired
	private MgrltrService mgrltrService;
		@Autowired
	private MgrLevelService mgrLevelService;
	
   /*
    * 显示页面
    * */
	@RequestMapping("/clientMgrInfoOther")
	public ModelAndView getAllInfoOtherByPage(int pageNum,HttpServletRequest req) throws ParseException {
		String mgrId = req.getParameter("mgrId");
		ModelAndView mv = new ModelAndView();
		List<MgrWorkResult> resultList = new ArrayList<MgrWorkResult>();
		List<MgrWorkHistory> workList = new ArrayList<MgrWorkHistory>();
		List<MgrCertificate> certificateList = new ArrayList<MgrCertificate>();
		List<MgrCheck> checkList = new ArrayList<MgrCheck>();
		List<MgrCmrpr> cmrprList = new ArrayList<MgrCmrpr>();
		List<MgrLearnTrain> learntrainList = new ArrayList<MgrLearnTrain>();
		List<MgrLevelAffirm> levelaffirmList = new ArrayList<MgrLevelAffirm>();
		if (pageNum == 1) {// 默认查询第一个page页的数据
			HashMap<String, Object> map = new HashMap<String, Object>();
			map.put("mgrId", mgrId);
			String niandu1 = req.getParameter("niandu1");
			map.put("cmWorkYear", niandu1);
			
			resultList = mgrWorkResultService.getWorkResultList(map);
			System.out.println("map================================="+map);
			System.out.println("resultList================================="+resultList);
			mv.addObject("resultList", resultList);
		} 
		else if(pageNum == 2) {
			String certi = req.getParameter("certi");
			HashMap<String, Object> map = new HashMap<String, Object>();
			map.put("mgrId", mgrId);
			map.put("certificatemame",certi);
			certificateList=mgrCertificateService.getCertificateList(map);
			mv.addObject("certificate", certificateList);
		}
		else if(pageNum == 3) {
			String rprclass = req.getParameter("rprclass");
			HashMap<String, Object> map = new HashMap<String, Object>();
			map.put("mgrId", mgrId);
			map.put("tpye",rprclass);
			cmrprList=mgrCmrprService.getrprList(map);
			mv.addObject("cmrprList", cmrprList);
			
		}
		else if(pageNum == 4) {
			String ctime = req.getParameter("ctime");
			String neirong=req.getParameter("neirong");
			HashMap<String, Object> map = new HashMap<String, Object>();
			map.put("mgrId", mgrId);
			map.put("neirong", neirong);
			/*if(ctime !="")
			{SimpleDateFormat sdfDateFormat=new SimpleDateFormat("yyyy-MM-dd");
			map.put("asstime", sdfDateFormat.parse(ctime));}*/
			checkList=mgrCheckService.getCheckList(map);
			mv.addObject("checkList", checkList);
		}
		else if(pageNum == 5) {
			String ptime = req.getParameter("ptime");
			String kemu = req.getParameter("kemu");
			HashMap<String, Object> map = new HashMap<String, Object>();
			/*if(ptime !="")
			{SimpleDateFormat sdfDateFormat=new SimpleDateFormat("yyyy-MM-dd");
			map.put("ptime",sdfDateFormat.parse(ptime));
			}*/
			map.put("mgrId", mgrId);
			map.put("sub", kemu);
			
			learntrainList=mgrltrService.getltrList(map);
			mv.addObject("learntrainList", learntrainList);
		}
		else if(pageNum == 6) {
			String rentime = req.getParameter("rentime");
			String renclass = req.getParameter("renclass");
			HashMap<String, Object> map = new HashMap<String, Object>();
			/*if(rentime != "")
			{SimpleDateFormat sdfDateFormat=new SimpleDateFormat("yyyy-MM-dd");
			map.put("rentime",sdfDateFormat.parse(rentime));}*/
			map.put("mgrId", mgrId);
			
			map.put("renclass",renclass);
			levelaffirmList=mgrLevelService.getlevelList(map);
			mv.addObject("levelaffirmList", levelaffirmList);
		}
	
		
		else if (pageNum == 7) {
			HashMap<String, Object> map = new HashMap<String, Object>();
			String zhiwu = req.getParameter("danwei");
			map.put("mgrId", mgrId);
			map.put("zhiwu",zhiwu);
			workList = mgrWorkHistoryService.getWorkList(map);
			mv.addObject("workList", workList);
		}
		
		mv.addObject("mgrId", mgrId);
		mv.addObject("pageNum", pageNum);
		mv.setViewName("/manager/clientMgrInfoOther.jsp");
		return mv;
	}

	@ModelAttribute("mgrLevel")
	public Map<String, String> getMgrLevelList() {
		Map<String, String> levelParam = new HashMap<String, String>();
		List<Param> paramList = paramService
				.getParamList(Constant.PARAM_TYPE_MGRLEVEL);
		if (paramList != null && paramList.size() > 0) {
			for (int i = 0; i < paramList.size(); i++) {
				Param param = paramList.get(i);
				levelParam.put(param.getParamCode(), param.getParamName());
			}
		}
		return levelParam;
	}
	@RequestMapping("modifyresult")
	public @ResponseBody List<MgrWorkResult> getResult(String cmKey) {
		List<MgrWorkResult> workLista =mgrWorkResultService.getresultBykey(cmKey);
		return workLista;
	}
	@RequestMapping("/mgrResultupdate")
	public String mgrResultupdate(MgrWorkResult work, Model model) {
		
		Date today = new Date();
		work.setModifyDate(today);
		mgrWorkResultService.workResultupdate(work);
		return "redirect:clientMgrInfoOther?pageNum=1&mgrId="+work.getMgrId();
	}
	@RequestMapping("modifycer")
	public @ResponseBody List<MgrCertificate> getcer(String cmKey) {
		List<MgrCertificate> cera =mgrCertificateService.getcerBykey(cmKey);

		return cera;
	}
	@RequestMapping("/mgrcerupdate")
	public String mgrcerupdate(MgrCertificate work, Model model) {
		
		Date today = new Date();
		work.setModifyDate(today);
		mgrCertificateService.cerupdate(work);
		System.out.println(work.toString());
		System.out.println("work.getMgrId()============================"+work.getMgrId());
		return "redirect:clientMgrInfoOther?pageNum=2&mgrId="+work.getMgrId();
	    
	}
	
	@RequestMapping("modifyrpr")
	public @ResponseBody List<MgrCmrpr> getrpr(String cmKey) {
		List<MgrCmrpr> cera =mgrCmrprService.getrprByKey(cmKey);

		return cera;
	}
	@RequestMapping("/mgrrprupdate")
	public String mgrrprupdate(MgrCmrpr work, Model model,MultipartFile upload,HttpServletRequest req) {
		String uploadFile_name = "";
		String newFileName = "";
		try {
			if (upload != null) {
				uploadFile_name = upload.getOriginalFilename();
				
				if (!"".equals(uploadFile_name))
					newFileName = UUID.randomUUID().toString()
							+ uploadFile_name.substring(uploadFile_name
									.lastIndexOf("."));
				
				String url = req.getSession().getServletContext()
						.getRealPath("/mgrWorkHist/uploadrpr");
				File uploadfile = new java.io.File(url + "/" + newFileName);
				if (!uploadfile.exists()) {
					uploadfile.mkdirs();
				}
				upload.transferTo(uploadfile);
				work.setAttachment(newFileName);
			}
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
		Date today = new Date();
		work.setModifyDate(today);
		mgrCmrprService.updaterpr(work);
		return "redirect:clientMgrInfoOther?pageNum=3&mgrId="+work.getMgrId();
	    
	}
	@RequestMapping("modifcheck")
	public @ResponseBody List<MgrCheck> getcheck(String cmKey) {
		List<MgrCheck> cera =mgrCheckService.getcheckByKey(cmKey);

		return cera;
	}
	@RequestMapping("/mgrcheckupdate")
	public String mgrcheckupdate(MgrCheck work, Model model) {
		
		Date today = new Date();
		work.setModifyDate(today);
	mgrCheckService.checkupdate(work);
		return "redirect:clientMgrInfoOther?pageNum=4&mgrId="+work.getMgrId();
	    
	}
	@RequestMapping("modifltr")
	public @ResponseBody List<MgrLearnTrain> getltr(String cmKey) {
		List<MgrLearnTrain> cera =mgrltrService.getltrByKey(cmKey);

		return cera;
	}
	@RequestMapping("/mgrltrupdate")
	public String mgrltrupdate(MgrLearnTrain work, Model model) {
		
		Date today = new Date();
		work.setModifyDate(today);
	mgrltrService.updateltr(work);
		return "redirect:clientMgrInfoOther?pageNum=5&mgrId="+work.getMgrId();
	    
	}
	@RequestMapping("modiflevel")
	public @ResponseBody List<MgrLevelAffirm> getlevel(String cmKey) {
		List<MgrLevelAffirm> cera =mgrLevelService.getLevelByKey(cmKey);
          
		
		return cera;
	}
	@RequestMapping("/mgrlevelupdate")
	public String mgrlevelupdate(MgrLevelAffirm work, Model model,MultipartFile upload,HttpServletRequest req) {
		String uploadFile_name = "";
		String newFileName = "";
		try {
			if (upload != null) {
				uploadFile_name = upload.getOriginalFilename();
				
				if (!"".equals(uploadFile_name))
					newFileName = UUID.randomUUID().toString()
							+ uploadFile_name.substring(uploadFile_name
									.lastIndexOf("."));
				
				String url = req.getSession().getServletContext()
						.getRealPath("/mgrWorkHist/uploadlevel");
				File uploadfile = new java.io.File(url + "/" + newFileName);
				if (!uploadfile.exists()) {
					uploadfile.mkdirs();
				}
				upload.transferTo(uploadfile);
				work.setAttachment(newFileName);
			}
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
		Date today = new Date();
		work.setModifyDate(today);
	mgrLevelService.levelupdate(work);
		return "redirect:clientMgrInfoOther?pageNum=6&mgrId="+work.getMgrId();
	    
	}
	
	@RequestMapping("/modifyWork")
	public @ResponseBody List<MgrWorkHistory> getWorkHistory(String cmKey) {
		List<MgrWorkHistory> workList = mgrWorkHistoryService
				.getWorkByKey(cmKey);
		System.out.println("modifyWorkdecmKey================="+cmKey);
		return workList;
	}
	 /*
	    * 更新工作经历
	    * */
	@RequestMapping("/updateWorkHistory")
	public String updateWorkHistory(MgrWorkHistory hist, Model model,MultipartFile upload,HttpServletRequest req) {
		String uploadFile_name = "";
		String newFileName = "";
		try {
			if (upload != null) {
				uploadFile_name = upload.getOriginalFilename();
				
				if (!"".equals(uploadFile_name))
					newFileName = UUID.randomUUID().toString()
							+ uploadFile_name.substring(uploadFile_name
									.lastIndexOf("."));
				
				String url = req.getSession().getServletContext()
						.getRealPath("/mgrWorkHist/upload");
				File uploadfile = new java.io.File(url + "/" + newFileName);
				if (!uploadfile.exists()) {
					uploadfile.mkdirs();
				}
				upload.transferTo(uploadfile);
				hist.setCmAttach(newFileName);
			}
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
		Date today = new Date();
		hist.setModifyDate(today);
		mgrWorkHistoryService.updateWorkHist(hist);
		return "redirect:clientMgrInfoOther?pageNum=7&mgrId="+hist.getMgrId();
	}
	 /*
	    * 增加年度工作业绩
	    * */
	@RequestMapping("/mgrResultAdd")
	public String mgrResultAdd(MgrWorkResult work, Model model) {
		
		Date today = new Date();
		work.setModifyDate(today);
		mgrWorkResultService.workResultAdd(work);
		return "redirect:clientMgrInfoOther?pageNum=1&mgrId="+work.getMgrId();
	}
	/*
	    * 增加证书记录
	    * */
	@RequestMapping("/mgrCertificateAdd")
	public String mgrCertificate(MgrCertificate mgrCertificate,Model model )
{          
		Date today = new Date();
	mgrCertificate.setModifyDate(today);
	mgrCertificateService.certificateAdd(mgrCertificate);
	return "redirect:clientMgrInfoOther?pageNum=2&mgrId="+mgrCertificate.getMgrId();}
	/*
	    * 增加惩奖记录
	    * */
	@RequestMapping("/mgrCmrprAdd")
	public String mgrCmrpr(MgrCmrpr mgrCmrpr,Model model,MultipartFile upload,HttpServletRequest req)
	{
		String uploadFile_name = "";
		String newFileName = "";
		try {
			if (upload != null) {
				uploadFile_name = upload.getOriginalFilename();
				
				if (!"".equals(uploadFile_name))
					newFileName = UUID.randomUUID().toString()
							+ uploadFile_name.substring(uploadFile_name
									.lastIndexOf("."));
				
				String url = req.getSession().getServletContext()
						.getRealPath("/mgrWorkHist/uploadrpra");
				File uploadfile = new java.io.File(url + "/" + newFileName);
				if (!uploadfile.exists()) {
					uploadfile.mkdirs();
				}
				upload.transferTo(uploadfile);
				mgrCmrpr.setAttachment(newFileName);
				System.out.println("mgrCmrpr.attachement"+newFileName);
			}
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		Date today = new Date();
	mgrCmrpr.setModifyDate(today);
	mgrCmrprService.mgrrpraddAdd(mgrCmrpr);
	return "redirect:clientMgrInfoOther?pageNum=3&mgrId="+mgrCmrpr.getMgrId();}
	
	/*
	    * 增加考核记录
	    * */

	@RequestMapping("/mgrCheckAdd")
	public String mgrCheck(MgrCheck mgrCheck,Model model)
	{Date today = new Date();
	mgrCheck.setModifyDate(today);
	mgrCheckService.checkAdd(mgrCheck);
	return "redirect:clientMgrInfoOther?pageNum=4&mgrId="+mgrCheck.getMgrId();}
	/*
	 * 增加学习培训
	 * */
	@RequestMapping("/mgrLearnTrainAdd")
	public String mgrCheck(MgrLearnTrain mgrLearnTrain,Model model)
	{Date today = new Date();
	mgrLearnTrain.setModifyDate(today);
	mgrltrService.ltrAdd(mgrLearnTrain);
	return "redirect:clientMgrInfoOther?pageNum=5&mgrId="+mgrLearnTrain.getMgrId();}
	/*
	    * 增加等级认证记录
	    * */
	@RequestMapping("/mgrLevelAffirmAdd")
	public String mgrLevel(MgrLevelAffirm mgrLevel,Model model,MultipartFile upload,HttpServletRequest req)
	{String uploadFile_name = "";
	String newFileName = "";
	try {
		if (upload != null) {
			uploadFile_name = upload.getOriginalFilename();
			
			if (!"".equals(uploadFile_name))
				newFileName = UUID.randomUUID().toString()
						+ uploadFile_name.substring(uploadFile_name
								.lastIndexOf("."));
			
			String url = req.getSession().getServletContext()
					.getRealPath("/mgrWorkHist/uploadlevela");
			File uploadfile = new java.io.File(url + "/" + newFileName);
			if (!uploadfile.exists()) {
				uploadfile.mkdirs();
			}
			upload.transferTo(uploadfile);
			mgrLevel.setAttachment(newFileName);
		}
	} catch (IllegalStateException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
		Date today = new Date();
	mgrLevel.setModifyDate(today);
	mgrLevelService.levelAdd(mgrLevel);
	return "redirect:clientMgrInfoOther?pageNum=6&mgrId="+mgrLevel.getMgrId();}
	/*
	    * 增加工作经历
	    * */
	@RequestMapping("/mgrWorkHistoryAdd")
	public String mgrLevel(MgrWorkHistory mgrWorkHistory,Model model,MultipartFile upload,HttpServletRequest req)
	{String uploadFile_name = "";
	String newFileName = "";
	try {
		if (upload != null) {
			uploadFile_name = upload.getOriginalFilename();
			
			if (!"".equals(uploadFile_name))
				newFileName = UUID.randomUUID().toString()
						+ uploadFile_name.substring(uploadFile_name
								.lastIndexOf("."));
			
			String url = req.getSession().getServletContext()
					.getRealPath("/mgrWorkHist/uploada");
			File uploadfile = new java.io.File(url + "/" + newFileName);
			if (!uploadfile.exists()) {
				uploadfile.mkdirs();
			}
			upload.transferTo(uploadfile);
			mgrWorkHistory.setCmAttach(newFileName);
		}
	} catch (IllegalStateException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
		Date today = new Date();
		
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		/*String time = format.format(mgrWorkHistory.getOfficeDate());
		mgrWorkHistory.setOfficeDate((Date)time);*/
	mgrWorkHistory.setModifyDate(today);
	mgrWorkHistoryService.workHistoryAdd(mgrWorkHistory);
	return "redirect:clientMgrInfoOther?pageNum=7&mgrId="+mgrWorkHistory.getMgrId();}

	@RequestMapping("deleteWorkResult")
	public String deleteWorkResult(String keys) {
		String usersId[]=keys.split(",");
		int user_ids[]=new int[usersId.length];
	    for(int i=0;i<usersId.length;i++)
	    {user_ids[i]=Integer.parseInt(usersId[i]);}
	    List<MgrWorkResult> workLista =mgrWorkResultService.getresultBykey(usersId[0]);
	    mgrWorkResultService.deleteResult(user_ids);
	    
	   
	    return "redirect:clientMgrInfoOther?pageNum=1&mgrId="+ workLista.get(0).getMgrId();}
	@RequestMapping("deletecer")
	public String deletecer(String keys) {
		String usersId[]=keys.split(",");
		int user_ids[]=new int[usersId.length];
	    for(int i=0;i<usersId.length;i++)
	    {user_ids[i]=Integer.parseInt(usersId[i]);}
	    List<MgrCertificate> lista=mgrCertificateService.getcerBykey(usersId[0]);
	    mgrCertificateService.deletecertificate(user_ids);
	    return "redirect:clientMgrInfoOther?pageNum=2&mgrId="+lista.get(0).getMgrId();}
	@RequestMapping("deleterpr")
	public String deleterpr(String keys) {
		String usersId[]=keys.split(",");
		int user_ids[]=new int[usersId.length];
	    for(int i=0;i<usersId.length;i++)
	    {user_ids[i]=Integer.parseInt(usersId[i]);}
	    List<MgrCmrpr> lista=mgrCmrprService.getrprByKey(usersId[0]);
	    mgrCmrprService.deleterpr(user_ids);
	    return "redirect:clientMgrInfoOther?pageNum=3&mgrId="+lista.get(0).getMgrId();}
	@RequestMapping("deletecheck")
	public String deletecheck(String keys) {
		String usersId[]=keys.split(",");
		int user_ids[]=new int[usersId.length];
	    for(int i=0;i<usersId.length;i++)
	    {user_ids[i]=Integer.parseInt(usersId[i]);}
	    List<MgrCheck> lista=mgrCheckService.getcheckByKey(usersId[0]);
	    mgrCheckService.deletecheck(user_ids);
	    return "redirect:clientMgrInfoOther?pageNum=4&mgrId="+lista.get(0).getMgrId();}
	@RequestMapping("deleteltr")
	public String deleteltr(String keys) {
		String usersId[]=keys.split(",");
		int user_ids[]=new int[usersId.length];
	    for(int i=0;i<usersId.length;i++)
	    {user_ids[i]=Integer.parseInt(usersId[i]);}
	    List<MgrLearnTrain> lista=mgrltrService.getltrByKey(usersId[0]); 
	    mgrltrService.deleteltr(user_ids);
	    return "redirect:clientMgrInfoOther?pageNum=5&mgrId="+lista.get(0).getMgrId();}
	@RequestMapping("deletelevel")
	public String deletelevel(String keys) {
		String usersId[]=keys.split(",");
		int user_ids[]=new int[usersId.length];
	    for(int i=0;i<usersId.length;i++)
	    {user_ids[i]=Integer.parseInt(usersId[i]);}
	    List<MgrLevelAffirm> lista=mgrLevelService.getLevelByKey(usersId[0]);
	    mgrLevelService.deletelevel(user_ids);
	    return "redirect:clientMgrInfoOther?pageNum=6&mgrId="+lista.get(0).getMgrId();}
	@RequestMapping("deleteWorkHist")
	public String deleteWorkHist(String keys) {
	String usersId[]=keys.split(",");
	int user_ids[]=new int[usersId.length];
    for(int i=0;i<usersId.length;i++)
    {user_ids[i]=Integer.parseInt(usersId[i]);}
    List<MgrWorkHistory> lista=mgrWorkHistoryService.getWorkByKey(usersId[0]);
   //Service
    mgrWorkHistoryService.deleteWorkHistory(user_ids);
  return "redirect:clientMgrInfoOther?pageNum=7&mgrId="+lista.get(0).getMgrId();}

}